package model

import (
	"gorm.io/gorm"
	"wechat/handle/response"
)

type Black struct {
	gorm.Model
	UserId   int `gorm:"type:int" json:"userId"`
	FriendId int `gorm:"type:int" json:"friendId"`
}

// 添加黑名单
func (b *Black) AddBlack(db *gorm.DB) error {
	return db.Debug().Create(&b).Error
}

// 验证黑名单
func (b *Black) VeryBlack(db *gorm.DB, userId int, friendId int) error {
	return db.Debug().Where("user_id =? and friend_id =?", userId, friendId).First(&b).Error
}

// 黑名单列表
func (b *Black) ListBlack(db *gorm.DB, userId int) (list []*response.ListBlack, err error) {
	err = db.Debug().Model(&b).Select("users. *", "users.id as FriendId", "users.phone as Phone", "users.username as Username", "users.image as Image", "users.created_at as Created").
		Joins("left join users on users.id = blacks.friend_id").
		Where("blacks.user_id=?", userId).Find(&list).Error
	return list, err
}
